
<!DOCTYPE HTML>
<html lang="cn" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>websocket · 前端学习笔记</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="chengdaohua">
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-anchors/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-toggle-chapters/toggle.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-advanced-emoji/emoji-website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="../webpack/webpack.html" />
    
    
    <link rel="prev" href="../ts/ts.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    
    
        
        <li>
            <a href="https://chengdaohua.gitee.io/remarkbook/" target="_blank" class="custom-link">Home</a>
        </li>
    
    

    
    <li class="divider"></li>
    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../vue/">
            
                <a href="../vue/">
            
                    
                    vue 学习
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../vue/start.html">
            
                <a href="../vue/start.html">
            
                    
                    原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../vue/mokuaihua.html">
            
                <a href="../vue/mokuaihua.html">
            
                    
                    模块化和组件化开发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../vue/huakucha.html">
            
                <a href="../vue/huakucha.html">
            
                    
                    花裤衩项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../vue/chushihua.html">
            
                <a href="../vue/chushihua.html">
            
                    
                    项目初始化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="../vue/guifan.html">
            
                <a href="../vue/guifan.html">
            
                    
                    代码规范
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../js/jsReadme.html">
            
                <a href="../js/jsReadme.html">
            
                    
                    javaScript
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../js/array.html">
            
                <a href="../js/array.html">
            
                    
                    数组
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../js/object.html">
            
                <a href="../js/object.html">
            
                    
                    对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../js/function.html">
            
                <a href="../js/function.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../js/string.html">
            
                <a href="../js/string.html">
            
                    
                    字符串
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../review/project.html">
            
                <a href="../review/project.html">
            
                    
                    review
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../review/CodeReview.html">
            
                <a href="../review/CodeReview.html">
            
                    
                    CoderReview
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../node/简介.html">
            
                <a href="../node/简介.html">
            
                    
                    node
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../node/简介.html">
            
                <a href="../node/简介.html">
            
                    
                    简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../node/express.html">
            
                <a href="../node/express.html">
            
                    
                    express
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../ts/ts.html">
            
                <a href="../ts/ts.html">
            
                    
                    ts
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.7" data-path="websocket.html">
            
                <a href="websocket.html">
            
                    
                    websocket
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../webpack/webpack.html">
            
                <a href="../webpack/webpack.html">
            
                    
                    webpack
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >websocket</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h3 id="websocket"><a name="websocket" class="plugin-anchor" href="#websocket"><i class="fa fa-link" aria-hidden="true"></i></a>WebSocket</h3>
<h4 id="1&#xFF0C;&#x4E3A;&#x5565;&#x9700;&#x8981;-websocket"><a name="1&#xFF0C;&#x4E3A;&#x5565;&#x9700;&#x8981;-websocket" class="plugin-anchor" href="#1&#xFF0C;&#x4E3A;&#x5565;&#x9700;&#x8981;-websocket"><i class="fa fa-link" aria-hidden="true"></i></a>1&#xFF0C;&#x4E3A;&#x5565;&#x9700;&#x8981; WebSocket</h4>
<ul>
<li>http &#x534F;&#x8BAE;&#x53EA;&#x80FD;&#x662F;&#x5BA2;&#x6237;&#x7AEF;&#x5411;&#x670D;&#x52A1;&#x5668;&#x53D1;&#x9001;&#x8BF7;&#x6C42;&#xFF0C;&#x670D;&#x52A1;&#x5668;&#x8FD4;&#x56DE;&#x67E5;&#x8BE2;&#x7ED3;&#x679C;&#xFF0C;http &#x534F;&#x8BAE;&#x505A;&#x4E0D;&#x5230;&#x670D;&#x52A1;&#x5668;&#x4E3B;&#x52A8;&#x5411;&#x5BA2;&#x6237;&#x7AEF;&#x53D1;&#x9001;&#x4FE1;&#x606F;</li>
<li>&#x5355;&#x5411;&#x8BF7;&#x6C42;&#x7684;&#x7F3A;&#x70B9;&#xFF1A;&#x5982;&#x679C;&#x670D;&#x52A1;&#x5668;&#x6709;&#x8FDE;&#x7EED;&#x6027;&#x72B6;&#x6001;&#x53D8;&#x5316;&#xFF0C;&#x53EA;&#x80FD;&#x6BCF;&#x9694;&#x4E00;&#x6BB5;&#x65F6;&#x95F4;&#x5C31;&#x53BB;&#x8BF7;&#x6C42;&#x4E00;&#x6B21;&#xFF0C; &#x5BFC;&#x81F4;&#x6548;&#x7387;&#x4F4E;&#xFF0C;&#x975E;&#x5E38;&#x6D6A;&#x8D39;&#x8D44;&#x6E90;</li>
</ul>
<h4 id="2&#xFF0C;&#x7B80;&#x4ECB;&#x548C;&#x7279;&#x70B9;"><a name="2&#xFF0C;&#x7B80;&#x4ECB;&#x548C;&#x7279;&#x70B9;" class="plugin-anchor" href="#2&#xFF0C;&#x7B80;&#x4ECB;&#x548C;&#x7279;&#x70B9;"><i class="fa fa-link" aria-hidden="true"></i></a>2&#xFF0C;&#x7B80;&#x4ECB;&#x548C;&#x7279;&#x70B9;</h4>
<ul>
<li>08 &#x5E74;&#x4EA7;&#x751F; 11 &#x5E74;&#x6210;&#x4E3A;&#x56FD;&#x9645;&#x6807;&#x51C6; &#x6D4F;&#x89C8;&#x5668;&#x90FD;&#x53EF;&#x4EE5;&#x652F;&#x6301;</li>
<li>&#x7279;&#x70B9;&#x662F;&#x670D;&#x52A1;&#x5668;&#x53EF;&#x4EE5;&#x5411;&#x5BA2;&#x6237;&#x7AEF;&#x4E3B;&#x52A8;&#x63A8;&#x9001;&#x4FE1;&#x606F;&#xFF0C;&#x5BA2;&#x6237;&#x7AEF;&#x4E5F;&#x80FD;&#x4E3B;&#x52A8;&#x8BF7;&#x6C42;&#x4FE1;&#x606F; &#x5E73;&#x7B49;&#x5BF9;&#x8BDD;</li>
<li>&#x5EFA;&#x7ACB;&#x5728; tcp &#x534F;&#x8BAE;&#x4E4B;&#x4E0A;&#xFF0C;&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x5B9E;&#x73B0;&#x6BD4;&#x8F83;&#x7B80;&#x5355;</li>
<li>&#x4E0E; http &#x534F;&#x8BAE;&#x6709;&#x7740;&#x826F;&#x597D;&#x517C;&#x5BB9;&#x6027;&#xFF0C;&#x9ED8;&#x8BA4;&#x7AEF;&#x53E3;&#x662F; 80 &#x548C; 443&#xFF0C;&#x5E76;&#x4E14;&#x63E1;&#x624B;&#x91C7;&#x7528; http &#x534F;&#x8BAE;&#xFF0C;&#x4E0D;&#x5BB9;&#x6613;&#x88AB;&#x5C4F;&#x853D;&#xFF0C;&#x80FD;&#x901A;&#x8FC7;&#x5404;&#x79CD; http &#x4EE3;&#x7406;&#x670D;&#x52A1;&#x5668;</li>
<li>&#x6570;&#x636E;&#x683C;&#x5F0F;&#x6BD4;&#x8F83;&#x8F7B;&#x91CF;&#xFF0C;&#x6027;&#x80FD;&#x5F00;&#x9500;&#x5C0F;&#xFF0C;&#x901A;&#x4FE1;&#x9AD8;&#x6548;</li>
<li>&#x53EF;&#x53D1;&#x9001;&#x6587;&#x672C;&#xFF0C;&#x4E5F;&#x53EF;&#x53D1;&#x9001;&#x4E8C;&#x8FDB;&#x5236;&#x6570;&#x636E;</li>
<li>&#x6CA1;&#x6709;&#x540C;&#x6E90;&#x9650;&#x5236;&#xFF0C;&#x5BA2;&#x6237;&#x7AEF;&#x53EF;&#x4EE5;&#x548C;&#x4EFB;&#x610F;&#x670D;&#x52A1;&#x5668;&#x901A;&#x8BAF;</li>
<li>&#x534F;&#x8BAE;&#x6807;&#x8BC6;&#x662F; ws &#x52A0;&#x5BC6;&#x4E3A; wss &#x670D;&#x52A1;&#x5668;&#x7F51;&#x5740;&#x5C31;&#x662F; url</li>
</ul>
<h5 id="&#x793A;&#x4F8B;&#xFF1A;-wsbaiducom80pagepath"><a name="&#x793A;&#x4F8B;&#xFF1A;-wsbaiducom80pagepath" class="plugin-anchor" href="#&#x793A;&#x4F8B;&#xFF1A;-wsbaiducom80pagepath"><i class="fa fa-link" aria-hidden="true"></i></a>&#x793A;&#x4F8B;&#xFF1A; ws://baidu.com:80/page/path</h5>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> ws = <span class="hljs-keyword">new</span> WebSocket(&#x201C;wss:<span class="hljs-comment">//echo.websocket.org&#x201D;);</span>

ws.onopen = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">evt</span>) </span>{
<span class="hljs-built_in">console</span>.log(&#x201C;Connection open &#x2026;&#x201D;);
ws.send(&#x201C;Hello WebSockets!&#x201D;);
};

ws.onmessage = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">evt</span>) </span>{
<span class="hljs-built_in">console</span>.log( &#x201C;Received Message: &#x201C; + evt.data);
ws.close();
};

ws.onclose = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">evt</span>) </span>{
<span class="hljs-built_in">console</span>.log(&#x201C;Connection closed.&#x201D;);
};
</code></pre>
<h4 id="&#x5BA2;&#x6237;&#x7AEF;&#x7684;-api"><a name="&#x5BA2;&#x6237;&#x7AEF;&#x7684;-api" class="plugin-anchor" href="#&#x5BA2;&#x6237;&#x7AEF;&#x7684;-api"><i class="fa fa-link" aria-hidden="true"></i></a>&#x5BA2;&#x6237;&#x7AEF;&#x7684; api</h4>
<p>a&#xFF0C;wbbSocket &#x6784;&#x9020;&#x51FD;&#x6570;
var ws = new WebSocket(&#x2018;ws://localhost:8080&#x2019;);</p>
<p>&#x6267;&#x884C;&#x4E0A;&#x9762;&#x8FD9;&#x4E2A;&#x8BED;&#x53E5;&#xFF0C;&#x5BA2;&#x6237;&#x7AEF;&#x5C31;&#x4F1A;&#x548C;&#x670D;&#x52A1;&#x5668;&#x8FDB;&#x884C;&#x94FE;&#x63A5;</p>
<pre><code class="lang-javascript">a&#xFF0C;wbbSocket&#x6784;&#x9020;&#x51FD;&#x6570;
<span class="hljs-keyword">var</span> ws = <span class="hljs-keyword">new</span> WebSocket(&#x2018;ws:<span class="hljs-comment">//localhost:8080&#x2019;);</span>

&#x6267;&#x884C;&#x4E0A;&#x9762;&#x8FD9;&#x4E2A;&#x8BED;&#x53E5;&#xFF0C;&#x5BA2;&#x6237;&#x7AEF;&#x5C31;&#x4F1A;&#x548C;&#x670D;&#x52A1;&#x5668;&#x8FDB;&#x884C;&#x94FE;&#x63A5;
&#xFF08;<span class="hljs-number">1</span>&#xFF09;&#xFF0C;webSocket.readyState
readyState&#x5C5E;&#x6027;&#x8FD4;&#x56DE;&#x5B9E;&#x4F8B;&#x5BF9;&#x8C61;&#x7684;&#x5F53;&#x524D;&#x72B6;&#x6001;
a&#xFF0C;CONNECTING &#xFF1A;&#x503C;&#x4E3A;<span class="hljs-number">0</span>&#xFF0C;&#x6B63;&#x5728;&#x8FDE;&#x63A5;
b&#xFF0C;OPEN &#xFF1A;&#x503C;&#x4E3A;<span class="hljs-number">1</span>&#xFF0C;&#x8868;&#x793A;&#x8FDE;&#x63A5;&#x6210;&#x529F;&#xFF0C;&#x53EF;&#x4EE5;&#x901A;&#x8BAF;&#x4E86;
c&#xFF0C;CLOSING &#xFF1A;&#x503C;&#x4E3A;<span class="hljs-number">2</span>&#xFF0C; &#x8FDE;&#x63A5;&#x6B63;&#x5728;&#x5173;&#x95ED;
d&#xFF0C;CLOSED &#xFF1A;&#x503C;&#x4E3A;<span class="hljs-number">3</span>&#xFF0C;&#x8FDE;&#x63A5;&#x5DF2;&#x7ECF;&#x5173;&#x95ED;&#xFF0C;&#x6216;&#x8005;&#x8FDE;&#x63A5;&#x5931;&#x8D25;

&#x793A;&#x4F8B;&#xFF1A;
<span class="hljs-keyword">switch</span> (ws.readyState) {
<span class="hljs-keyword">case</span> WebSocket.CONNECTING:
<span class="hljs-comment">// do something</span>
<span class="hljs-keyword">break</span>;
<span class="hljs-keyword">case</span> WebSocket.OPEN:
<span class="hljs-comment">// do something</span>
<span class="hljs-keyword">break</span>;
<span class="hljs-keyword">case</span> WebSocket.CLOSING:
<span class="hljs-comment">// do something</span>
<span class="hljs-keyword">break</span>;
<span class="hljs-keyword">case</span> WebSocket.CLOSED:
<span class="hljs-comment">// do something</span>
<span class="hljs-keyword">break</span>;
<span class="hljs-keyword">default</span>:
<span class="hljs-comment">// this never happens</span>
<span class="hljs-keyword">break</span>;
}

&#xFF08;<span class="hljs-number">2</span>&#xFF09;webSocket.onopen
&#x6307;&#x5B9A;&#x8FDE;&#x63A5;&#x6210;&#x529F;&#x540E;&#x7684;&#x56DE;&#x8C03;&#x51FD;&#x6570;
&#x56DE;&#x8C03;&#x591A;&#x4E2A;&#x51FD;&#x6570; webCocked.addEventListener

&#xFF08;<span class="hljs-number">3</span>&#xFF09;webSocked.onclose
&#x6307;&#x5B9A;&#x8FDE;&#x63A5;&#x5173;&#x95ED;&#x540E;&#x7684;&#x56DE;&#x8C03;&#x51FD;&#x6570;
ws.onclose = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">event</span>) </span>{
<span class="hljs-keyword">var</span> code = event.code;
<span class="hljs-keyword">var</span> reason = event.reason;
<span class="hljs-keyword">var</span> wasClean = event.wasClean;
<span class="hljs-comment">// handle close event</span>
};

&#xFF08;<span class="hljs-number">4</span>&#xFF09;webSocket.onmessage
&#x7528;&#x4E8E;&#x6307;&#x5B9A;&#x6536;&#x5230;&#x670D;&#x52A1;&#x5668;&#x6570;&#x636E;&#x540E;&#x7684;&#x56DE;&#x8C03;&#x51FD;&#x6570;
ws.onmessage = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">event</span>) </span>{
<span class="hljs-keyword">var</span> data = event.data;
<span class="hljs-comment">// &#x5904;&#x7406;&#x6570;&#x636E;</span>
};
&#x670D;&#x52A1;&#x5668;&#x6570;&#x636E;&#x53EF;&#x80FD;&#x662F;&#x6587;&#x672C;&#xFF0C;&#x4E5F;&#x53EF;&#x80FD;&#x662F;&#x4E8C;&#x8FDB;&#x5236;&#x6570;&#x636E;&#xFF08;blob&#x5BF9;&#x8C61;&#x6216;Arraybuffer&#x5BF9;&#x8C61;&#xFF09;&#x3002;

ws.onmessage = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">event</span>)</span>{
<span class="hljs-keyword">if</span>(<span class="hljs-keyword">typeof</span> event.data === <span class="hljs-built_in">String</span>) {
<span class="hljs-built_in">console</span>.log(&#x201C;Received data string&#x201D;);
}

<span class="hljs-keyword">if</span>(event.data <span class="hljs-keyword">instanceof</span> <span class="hljs-built_in">ArrayBuffer</span>){
<span class="hljs-keyword">var</span> buffer = event.data;
<span class="hljs-built_in">console</span>.log(&#x201C;Received arraybuffer&#x201D;);
}
}

&#xFF08;<span class="hljs-number">5</span>&#xFF09;&#x9664;&#x4E86;&#x52A8;&#x6001;&#x5224;&#x65AD;&#x6536;&#x5230;&#x7684;&#x6570;&#x636E;&#x7C7B;&#x578B;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;binaryType&#x5C5E;&#x6027;&#xFF0C;&#x663E;&#x5F0F;&#x6307;&#x5B9A;&#x6536;&#x5230;&#x7684;&#x4E8C;&#x8FDB;&#x5236;&#x6570;&#x636E;&#x7C7B;&#x578B;&#x3002;
<span class="hljs-comment">// &#x6536;&#x5230;&#x7684;&#x662F; blob&#xFF08;&#x4E8C;&#x8FDB;&#x5236;&#x5927;&#x5BF9;&#x8C61;&#xFF0C;&#x662F;&#x4E00;&#x4E2A;&#x53EF;&#x4EE5;&#x5B58;&#x50A8;&#x4E8C;&#x8FDB;&#x5236;&#x6587;&#x4EF6;&#x7684;&#x5BB9;&#x5668;&#xFF09; &#x6570;&#x636E;</span>
ws.binaryType = &#x201C;blob&#x201D;;
ws.onmessage = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">e</span>) </span>{
<span class="hljs-built_in">console</span>.log(e.data.size);
};

<span class="hljs-comment">// &#x6536;&#x5230;&#x7684;&#x662F; ArrayBuffer &#x6570;&#x636E;</span>
ws.binaryType = &#x201C;arraybuffer&#x201D;;
ws.onmessage = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">e</span>) </span>{
<span class="hljs-built_in">console</span>.log(e.data.byteLength);
};

webSocket.send() &#x5411;&#x670D;&#x52A1;&#x5668;&#x53D1;&#x9001;&#x6570;&#x636E;&#x3002;
ws.send(&#x2018;your message&#x2019;);

webSocket.bufferedAmount &#x8868;&#x793A;&#x8FD8;&#x6709;&#x591A;&#x5C11;&#x5B57;&#x8282;&#x7684;&#x4E8C;&#x8FDB;&#x5236;&#x6570;&#x636E;&#x6CA1;&#x6709;&#x53D1;&#x9001;&#x51FA;&#x53BB;&#x3002;&#x5B83;&#x53EF;&#x4EE5;&#x7528;&#x6765;&#x5224;&#x65AD;&#x53D1;&#x9001;&#x662F;&#x5426;&#x7ED3;&#x675F;&#x3002;
<span class="hljs-keyword">var</span> data = <span class="hljs-keyword">new</span> <span class="hljs-built_in">ArrayBuffer</span>(<span class="hljs-number">10000000</span>);
socket.send(data);

<span class="hljs-keyword">if</span> (socket.bufferedAmount === <span class="hljs-number">0</span>) {
<span class="hljs-comment">// &#x53D1;&#x9001;&#x5B8C;&#x6BD5;</span>
} <span class="hljs-keyword">else</span> {
<span class="hljs-comment">// &#x53D1;&#x9001;&#x8FD8;&#x6CA1;&#x7ED3;&#x675F;</span>
}

webSocket.onerror
&#x7528;&#x4E8E;&#x6307;&#x5B9A;&#x62A5;&#x9519;&#x65F6;&#x7684;&#x56DE;&#x8C03;&#x51FD;&#x6570;
socket.onerror = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">event</span>) </span>{
<span class="hljs-comment">// handle error event</span>

};
</code></pre>
<footer class="page-footer"><span class="copyright">Copyright &#xA9; wml 2019 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x8BE5;&#x6587;&#x4EF6;&#x4FEE;&#x8BA2;&#x65F6;&#x95F4;&#xFF1A;
2020-05-22 14:08:23
</span></footer>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="../ts/ts.html" class="navigation navigation-prev " aria-label="Previous page: ts">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="../webpack/webpack.html" class="navigation navigation-next " aria-label="Next page: webpack">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"websocket","level":"1.7","depth":1,"next":{"title":"webpack","level":"1.8","depth":1,"path":"category/webpack/webpack.md","ref":"category/webpack/webpack.md","articles":[]},"previous":{"title":"ts","level":"1.6","depth":1,"path":"category/ts/ts.md","ref":"category/ts/ts.md","articles":[]},"dir":"ltr"},"config":{"plugins":["-lunr","anchors","sitemap-general","toggle-chapters","expandable-chapters","splitter","edit-link","advanced-emoji","tbfed-pagefooter"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy wml 2019","modify_label":"该文件修订时间：","modify_format":"YYYY-MM-DD HH:mm:ss"},"github":{"url":"https://gitee.com/chengdaohua/remarkBook"},"simple-page-toc":{"maxDepth":3,"skipFirstH1":false},"splitter":{},"search-pro":{"cutWordLib":"nodejieba","defineWord":["gitbook-use"]},"search":{},"donate":{"wechat":"","alipay":"","title":"","button":"赏","alipayText":"支付宝打赏","wechatText":"微信打赏"},"sitemap-general":{"prefix":"https://chengdaohua.gitee.io/remarkbook/"},"changyan":{"appid":"cytowLiqM","conf":"the conf in the code generate by changyan"},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"sitemap":{"hostname":"https://wmlhub.gitee.io/gitbook/"},"baidu":{"token":"084b900a1e26f6bae60e09d18e3aa455"},"advanced-emoji":{"embedEmojis":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":true,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"edit-link":{"label":"Edit This Page","base":"https://gitee.com/chengdaohua/remarkBook"},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"anchors":{},"toggle-chapters":{},"expandable-chapters":{}},"theme":"default","author":"chengdaohua","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"前端学习笔记","language":"cn","links":{"sidebar":{"Home":"https://chengdaohua.gitee.io/remarkbook/"}},"gitbook":"3.2.3","description":"记录日常学习总结"},"file":{"path":"category/websocket/websocket.md","mtime":"2020-05-22T06:08:23.551Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-05-22T06:18:21.707Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-toggle-chapters/toggle.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-edit-link/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

